<?php
class NewsController extends AppController {
	var $name = 'News';
	var $uses = array('news.News', 'news.NewsCategory');
	var $helpers = array('text');
	var $view = 'Theme';
	var $theme = 'qat';
	var $layout = 'news';
	
	/**
	 @var $model
	 @brief Thông tin về model, được sử dụng trong trường hợp override thông tin của danh mục
	 */
	var $models = array(
		'Category'	=>	array(
			'plugin'	=>	'news',
			'name'		=>	'NewsCategory'
		),
		'Page'		=>	array(
			'plugin'	=>	'news',
			'name'		=>	'News'
		),
	);
	function beforeFilter() {
		parent::beforeFilter();
		foreach ($this->models as $key => $item) {
			App::import('Model', $item['plugin'].".".$item['name']);
			$this->loadModel($item['name']);
		}
		$this->set('models', $this->models);
		// save contact us information
		$this->configs = $this->News->loadConfig();
		$this->set('configs', $this->configs);
	}
	//function index($alias = null) {
	function index() {
		$categories = $this->NewsCategory->children(1, false);
		
		for($i=0; $i< count($categories); $i++){
			$category = & $categories[$i];
			$category['top_news'] = $this->News->getTopNews($category['NewsCategory']['id'], array('id', 'alias', 'title', 'image', 'created', 'description'), 3);
			//debug($category['top_news']); exit;
		}
		//debug($categories); exit;
		$this->set('categories', $categories);
	}
	function view($alias, $category_alias = '') {
		if (!$alias) {
			$this->Session->setFlash(__("Không đúng mã tin", true), "flash_error");
			//$this->redirect(array('action'=>'index'));
		} else {
			$this->News->Behaviors->attach('Containable');
			$article = $this->News->find('first', array(
				'conditions'	=>	array(
					"News.alias" => $alias,
                    "News.is_published"	=>1
				),
				'fields'		=>	array('id', 'title', 'content', 'description', 'image', 'Category.id', 'news_date', 'alias'),
				'order'			=>	"News.priority desc",
				'joins'	=>	array(
					array(
						'table' => $this->News->hasAndBelongsToMany['Category']['joinTable'],
						'type' => 'INNER',
						'alias' => 'Middle',
						'conditions' => array(
							"Middle.page_id = News.id"
						)
					),
					array(
						'table' => $this->{$this->models['Category']['name']}->table,
						'type' => 'INNER',
						'alias' => 'Category',
						'conditions' => array(
							'Middle.category_id = Category.id'
						)
					),
				)
			));
			
			$this->set('article', $article);
			//debug($article);
			// Load Other News
			# Newer news
			$olderNews = $this->News->getNewerNews($alias, $category_alias, array('alias', 'title', 'news_date', 'description', 'image'), 3);
			
			# Older news
			$newerNews = $this->News->getOlderNews($alias, $category_alias, array('alias', 'title', 'news_date', 'description', 'image'), 3);

			$other_news = array_merge($newerNews, $olderNews);
			
            if (isset($this->params['named']['media']) && $this->params['named']['media'] == "print") {
				$this->render('view-print');
			}
            
			// Load Category information
			$category = $this->NewsCategory->find('first', array(
				'conditions'	=>	array('NewsCategory.alias' => $category_alias),
				'fields'		=>	array('name', 'alias')
			));
			
			$this->set('other_news', $other_news);
			$this->set('category_alias', $category_alias);
			$this->set('category', $category);
		}
	}
	function listnews($alias = null) {
		if($alias) {
			/*
			 * phần lấy dữ liệu cho danh mục tin tức
			 */
			$category = $this->NewsCategory->find('first', array(
				'conditions'	=> array("NewsCategory.alias" => $alias),
				'fields'		=> array('name', 'alias')
			));
			 $this->set('category', $category);
			 
			$list_news = $this->News->getPagesInCategory($this, $alias, array('title', 'alias', 'image', 'description', 'news_date', 'created'), array('limit' => 4));
            $this->set('list_news', $list_news);
            //debug($list_news); exit;
		}
    }
}
?>